<body class="iframe">
    <div class="add">
        <div class="frame-header">
            <h2><i class="layui-icon">&#xe705;</i>联系人管理</h2>
        </div>
        <h4 class="layui-bg-dark frame-title"><i class="layui-icon">&#xe65f;</i>添加收件人</h4>
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <ul class="layui-tab-title">
                <li class="layui-this">批量导入</li>
                <li>单条录入</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <h2>请导入excel或者txt文件</h2>
                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">文件地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="localexcel" required lay-verify="required" placeholder="文件路径" autocomplete="off" class="layui-input localurl">
                                <input type="hidden" name="severexcel" placeholder="文件路径" autocomplete="off" class="layui-input serverurl">
                            </div>
                            <button type="button" class="layui-btn" id="uploadsender"><i class="layui-icon">&#xe67c;</i>上传文件</button>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">设置分组</label>
                            <div class="layui-input-inline">
                                <select name="group" lay-verify="required" lay-filter='sender'>
                                    <?php foreach($group as $rows): ?>
                                    <option value="<?=$rows['id']?>"><?=$rows['name']?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="import">导入</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-tab-item">
                    <h2>请录入收件人信息</h2>
                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">邮箱</label>
                            <div class="layui-input-block">
                                <input type="text" name="email" required lay-verify="required|email" placeholder="请输入收件人邮箱" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">设置分组</label>
                            <div class="layui-input-inline">
                                <select name="group" lay-verify="required" lay-filter='sender'>
                                    <?php foreach($group as $rows): ?>
                                    <option value="<?=$rows['id']?>"><?=$rows['name']?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="importhandy">添加</button>
                            </div>
                        </div>
                    </form>

                </div>
            </div>
        </div>

    </div>
</body>
<script>
    layui.use(['element', 'form', 'upload', 'layer'], function () {
        var element = layui.element, form = layui.form, upload = layui.upload, layer = layui.layer;
        form.on('submit(import)', function (data) {
            let url = "<?= site_url('import/import_file_data') ?>";
            let jsonData = {
                'type': 2,
                'file': data.field.severexcel,
                'group':data.field.group
            };
            $.post(url, jsonData, function (redata) {
                if (redata.errno == '666') {
                    layer.msg(redata.error);
                } else {
                    layer.msg(redata.error);
                    return false;
                }
            }, 'json');
            return false;
        });

        form.on('submit(importhandy)', function (data) {
            let url = "<?= site_url('import/import_data_handy') ?>";
            let jsonData = {
                'type': 2,
                'email': data.field.email,
                'group':data.field.group
            };
            $.post(url, jsonData, function (redata) {
                if (redata.errno == '666') {
                    layer.msg(redata.error);
                } else {
                    layer.msg(redata.error);
                    return false;
                }
            }, 'json');
            return false;
        });


        //上传文件
        var uploadInst = upload.render({
            elem: '#uploadsender',
            accept: 'file',
            size: 8192, //最大支持8m的文件上传
            exts: 'txt|xls|xlsx',
            auto: false,
            url: '<?= site_url("upload/do_upload_address") ?>',
            choose: function (obj) {
                layer.confirm('确定导入此文件?', function (index) {
                    obj.preview(function (index, file, result) {
                        $(".localurl").val(file.name);
                        layer.load();
                        obj.upload(index, file);
                    });
                });
            },
            done: function (redata) {
                if (redata.errno == '666') {
                    layer.closeAll('loading');
                    layer.msg(redata.error);
                    $(".serverurl").val(redata.uploadInfo.upload_data.file_name);
                } else {
                    layer.closeAll('loading');
                    layer.msg(redata.error);
                }
            },
            error: function () {
                layer.msg('文件导入异常');
            }
        });




    });
</script>

</html>